package com.chenchao.javafile;

import java.io.*;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/15 17:01
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.javaFile.Test7.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 字节缓冲流
 */
public class Test7 {
    public static void main(String[] args) {
        //test1();
        test2();

    }

    /**
     * 字节流拷贝3335ms
     */
    public static void test1(){
        long startTime=System.currentTimeMillis();
        File file = new File("myjava01/src/fo/PYQT5.zip");
        File file2 = new File("myjava01/src/img/gg");
        try(FileInputStream fileInputStream = new FileInputStream(file);
            FileOutputStream fileOutputStream = new FileOutputStream(file2);)
        {
            byte[] bytes = new byte[1024];
            int len=0;
            while((len=fileInputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(System.currentTimeMillis()-startTime+"ms");
    }

    /**
     * 缓冲字节流拷贝1262ms
     */
    public static void test2(){
        long startTime=System.currentTimeMillis();
        File file = new File("myjava01/src/fo/PYQT5.zip");
        File file2 = new File("myjava01/src/img/gg2");
        //构造方法
        try(BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
             BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file2));)
        {
            byte[] bytes = new byte[1024];
            int len=0;
            while((len=bufferedInputStream.read(bytes))!=-1){
               bufferedOutputStream.write(bytes,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(System.currentTimeMillis()-startTime+"ms");

    }
}
